//
//  LimitInput.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/12/10.
//

import Foundation
import RxSwift

// MARK: - 限制输入
public enum LimitInputEnum {
    case money
    case phone
    case allowStr(String)
    case maxLength(Int)
}

public extension LimitInputEnum {
    
    /// 过滤器
    func filiter(_ str: String) -> String {
        var text = str
        switch self {
        case .money:
            text = money(str)
        case .phone:
            text = phone(str)
        case .allowStr(let string):
            text = allowStr(str, allowStrs: string)
        case .maxLength(let int):
            text = maxLength(str, maxLength: int)
        }
        return text
    }
    
    /// 手机号
    func phone(_ str: String) -> String {
        let text = allowStr(str, allowStrs: "0123456789")
        if text.isMatch(.phone) {
            return text
        }
        if !text.hasPrefix("1") {
            return ""
        }
        if text.count > 0 {
            return text.substring(to: 11)
        }
        return text
    }
    
    /// 金额输入
    func money(_ str: String) -> String {
        var text = allowStr(str, allowStrs: "0123456789.")
        if text.isMatch(.moneyPointCanEnd) {
            return text
        }
        if text.count > 0 {
            if text.hasPrefix(".") {
                text = "0." + text
                return money(text)
            }
            if text.countStr(".") >= 1 {
                let array = text.components(separatedBy: ".")
                text = array[0] + "." + array[1].substring(to: 2)
            }
            return text
        }
        return ""
    }
    
    /// 身份证<未验证>
    func idCard(_ str: String) -> String {
        var text = allowStr(str, allowStrs: "0123456789Xx")
        if text.isMatch(.idCard) {
            return text
        }
        if text.count > 18 {
            return text.substring(to: 18)
        }
        return ""
    }
    
    /// 限制输入字符串
    func allowStr(_ str: String, allowStrs: String) -> String {
        return str.components(separatedBy: CharacterSet(charactersIn: allowStrs).inverted).joined(separator: "")
    }
    
    /// 限制长度
    func maxLength(_ str: String, maxLength: Int) -> String {
        return str.substring(to: maxLength)
    }
    
}


public struct LimitInputHelper {
    
    /// text filed
    public static func tf(_ tf: UITextField, rules targetRules: [LimitInputEnum]? = nil, bag: DisposeBag, closure: ((String) -> Void)? = nil) {
        tf.rx.text.orEmpty.subscribe(onNext: {
            var text = $0
            if let rules = targetRules {
                text = LimitInputHelper.verifyStr(text, rules: rules)
            }
            if $0 != text {
                let ranage = tf.selectedTextRange
                tf.text = text
                tf.selectedTextRange = ranage
            }
            if let block = closure {
                block(text)
            }
        })
        .disposed(by: bag)
    }

    /// text view
    public static func tv(_ tv: UITextView, rules targetRules: [LimitInputEnum]? = nil, bag: DisposeBag, closure: ((String) -> Void)? = nil) {
        tv.rx.text.orEmpty.subscribe(onNext: {
            var text = $0
            if let rules = targetRules {
                text = LimitInputHelper.verifyStr(text, rules: rules)
            }
            if $0 != text {
                let ranage = tv.selectedTextRange
                tv.text = text
                tv.selectedTextRange = ranage
            }
            if let block = closure {
                block(text)
            }
        })
        .disposed(by: bag)
    }

    /// 循环限制输入
    private static func verifyStr(_ str: String, rules: [LimitInputEnum]) -> String {
        var text = str
        rules.forEach {
            text = $0.filiter(text)
        }
        return text
    }

}
